package com.allwees.bs.core.modelbase.page;

import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.List;

/**
 * 封装数据的page 对象
 *
 * @author yangylsky
 * @since 2019-10-11
 *
 * @param <T>
 */
@Data
@Accessors(chain=true)
public class PaginateDTO<T> implements Serializable {
	private static final long serialVersionUID = 2244718303845268054L;
	// 当前页
	private long current;

	// 最后一页
	private long lastPage;

	// 每页个数
	private int size;

	// 总数
	private long total;

	// 当前页的数量
	private int count;

	// 是否还有下一页
	private boolean hasMorePages;

	private List<T> results;

	/**
	 * 闪购商品剩余结束时长
	 */
	private Long remainMillis;

	/**
	 * 购物车是否含有free gift
	 */
	private String freeGiftUuid;

	public PaginateDTO() {
		this(0L, 0, new PageParams());
	}

	public PaginateDTO(long total, int count, PageParams pageParams) {
		this.total = total;
		this.count = count;
		this.current = pageParams.getCurrent();

		if(total % pageParams.getSize() == 0) {
			this.lastPage = total / pageParams.getSize();
		} else {
			this.lastPage = (total / pageParams.getSize()) + 1;
		}

		this.size = pageParams.getSize();
		this.hasMorePages = ((this.current - 1) * this.size + this.count) < this.total;
	}

	public static <T> PaginateDTO<T> of(long total, int count, PageParams pageParams) {
		return new PaginateDTO<>(total, count, pageParams);
	}

	public static <T> PaginateDTO<T> of(int total, int count, PageParams pageParams) {
		return new PaginateDTO<>(total, count, pageParams);
	}

//	public Collection<T> getResults(){
//		if(CollectionUtils.isEmpty(results)){
//			return new ArrayList<>();
//		}
//		return results;
//	}
}
